package com.forbet.demo.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.forbet.demo.mapper.AsianBankerInfoMapper;
import com.forbet.demo.mapper.EuropeBankerInfoMapper;
import com.forbet.demo.mapper.PlayInfoMapper;
import com.forbet.demo.po.AsianBankerInfoPO;
import com.forbet.demo.po.EuropeBankerInfoPO;
import com.forbet.demo.po.PlayInfoPO;
import com.forbet.demo.vo.ExportPlayInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.SpreadsheetVersion;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/play")
public class PlayInfoController {

    @Autowired
    private PlayInfoMapper playInfoMapper;

    @Autowired
    private AsianBankerInfoMapper asianBankerInfoMapper;

    @Autowired
    private EuropeBankerInfoMapper europeBankerInfoMapper;


//    public static final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7891));


    @RequestMapping(path = "/exportPlayInfoExcel", method = RequestMethod.POST)
    public void exportPlayInfoExcel() throws Exception {
        LambdaQueryWrapper<PlayInfoPO> playInfoPOLambdaQueryWrapper = new LambdaQueryWrapper<>();
        playInfoPOLambdaQueryWrapper.between(PlayInfoPO::getPlayingTime, DateUtil.beginOfDay(DateUtil.parse("2023-12-01")), DateUtil.endOfDay(DateUtil.parse("2023-12-21")));
//        playInfoPOLambdaQueryWrapper.eq(PlayInfoPO::getId, 1736930785980039192L);
        List<PlayInfoPO> playInfoPOS = playInfoMapper.selectList(playInfoPOLambdaQueryWrapper);
        List<ExportPlayInfoVO> exportPlayInfoVOS = new ArrayList<>();
        for (PlayInfoPO playInfoPO:playInfoPOS
             ) {
            ExportPlayInfoVO exportPlayInfoVO = new ExportPlayInfoVO();
            Long id = playInfoPO.getId();
            LambdaQueryWrapper<EuropeBankerInfoPO> europeBankerInfoPOLambdaQueryWrapper = new LambdaQueryWrapper<>();
            europeBankerInfoPOLambdaQueryWrapper.eq(EuropeBankerInfoPO::getPlayInfoId, id);
            europeBankerInfoPOLambdaQueryWrapper.eq(EuropeBankerInfoPO::getBankerName,"Bet365");
            europeBankerInfoPOLambdaQueryWrapper.orderByAsc(EuropeBankerInfoPO::getCreateTime);
            List<EuropeBankerInfoPO> europeBankerInfoPOList = europeBankerInfoMapper.selectList(europeBankerInfoPOLambdaQueryWrapper);
            if(europeBankerInfoPOList != null && europeBankerInfoPOList.size() > 0){
                JSONObject jsonObject = new JSONObject();
                jsonObject.set("odds", europeBankerInfoPOList);
                exportPlayInfoVO.setHomeName("主队名称:" + playInfoPO.getHomeName());
                exportPlayInfoVO.setVisitName("客队名称:" + playInfoPO.getVisitName());
                exportPlayInfoVO.setHomeScore("主队进球数量:" + playInfoPO.getHomeScore());
                exportPlayInfoVO.setVisitScore("客队进球数量:" + playInfoPO.getVisitScore());
                exportPlayInfoVO.setOdds("欧盘赔率变化:" + jsonObject.toString());
                exportPlayInfoVOS.add(exportPlayInfoVO);
            }
        }
        initCellMaxTextLength();
        EasyExcel.write("gpt机器人文件.xlsx",ExportPlayInfoVO.class).sheet().doWrite(exportPlayInfoVOS);
//        EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
    }

    /**
     * 初始化 cell 内容长度
     * 	   cell 原本内容长度限制 32767  现修改为Integer.MAX_VALUE
     */
    public static void initCellMaxTextLength() {
        SpreadsheetVersion excel2007 = SpreadsheetVersion.EXCEL2007;
        if (Integer.MAX_VALUE != excel2007.getMaxTextLength()) {
            Field field;
            try {
                field = excel2007.getClass().getDeclaredField("_maxTextLength");
                field.setAccessible(true);
                field.set(excel2007,Integer.MAX_VALUE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
